package utils
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.media.Sound;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.utils.ByteArray;
	
	public class Library extends Sprite
	{
		private var appDomain		:ApplicationDomain;
		private var loader			:Loader;
		private var loaderContext 	:LoaderContext;		// AIR 용으로 추가 by sestival
		private var func			:Function;
		
		public function Library(content:* = null, func:Function = null)
		{
			try{
				this.func = func;
				if(content != null){
					load(content);
				}
			}catch(e:Error){
				trace(e.getStackTrace());
			}
		}
		
		public function load(content:* = null):void{
			try{
				if(content == null) throw new Error("컨텐츠가 없습니다.");
				loader = new Loader();
				loaderContext = new LoaderContext();
				//loaderContext.allowLoadBytesCodeExecution = true;		// 이부분을 추가해 줘야 AIR 에서도 사용가능함
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
				if(content is ByteArray){//바이트라면
					loader.loadBytes(content, loaderContext);
				}else if(content is URLRequest){//주소이라면
					loader.load(content);
				}
			}catch(e:Error){
				trace(e.getStackTrace());
			}
		}
		
		private function onComplete(e:Event = null):void
		{
			try{
				appDomain = e.target.applicationDomain;
				if(func != null){
					func();
				}else{
					dispatchEvent(new Event(Event.INIT));
				}
			}catch(e:Error){
				trace(e.getStackTrace());
			}
		}
		
		public function getAppDomain():ApplicationDomain{
			return appDomain;
		}
		
		//클래스
		public function getClass(name:String = null):Class{
			return appDomain.getDefinition(name) as Class;
		}
		
		//디스플레이 오브젝트 스테이지에 바로
		public function addDisplayObject(name:String, _root:Sprite):Boolean{
			try{
				var _class:Class = appDomain.getDefinition(name) as Class;
				_root.addChild(new _class() as DisplayObject);
			}catch(e:Error){
				trace(e.getStackTrace());
				return false;
			} 
			return true;
		}
		
		//디스플레이 오브젝트
		public function getDisplayObject(name:String):DisplayObject{
			var rtn:DisplayObject; 
			try{
				var _class:Class = appDomain.getDefinition(name) as Class;
				rtn = new _class() as DisplayObject;
			}catch(e:Error){
				trace(e.getStackTrace());
			} 
			return rtn;
		}
		
		//무비클립
		public function getMovieClip(name:String):MovieClip{
			var rtn:MovieClip; 
			try{
				var _class:Class = appDomain.getDefinition(name) as Class;
				rtn = new _class() as MovieClip;
			}catch(e:Error){
				trace(e.getStackTrace());
			} 
			return rtn;
		}


		//비트맵
		public function getBitmap(name:String):Bitmap{
			var rtn:Bitmap; 
			try{
				var _class:Class = appDomain.getDefinition(name) as Class;
				rtn = new Bitmap(new _class(0, 0) as BitmapData);
			}catch(e:Error){
				trace(e.getStackTrace());
			} 
			return rtn;
		}

		//비트맵 데이타
		public function getBitmapData(name:String):BitmapData{
			var rtn:BitmapData; 
			try{
				var _class:Class = appDomain.getDefinition(name) as Class;
				rtn = new _class(0, 0) as BitmapData;
			}catch(e:Error){
				trace(e.getStackTrace());
			} 
			return rtn;
		}
		
		//데이타 존재하는지 체크
		public function hasAssets( name:String ):Boolean{ 
			return appDomain.hasDefinition( name );
		}

		//사운드
		public function getSound(name:String):Sound{
			var rtn:Sound;
			try{
				var _class:Class = appDomain.getDefinition(name) as Class;
				rtn = new _class() as Sound;
			}catch(e:Error){
				trace(e.getStackTrace());
			} 
			return rtn;			
		}
	}
}